Skip to content

fix: copy sudo binary before setting setuid bit#9

Merged
megheaiulian merged 1 commit intomainfrom
fix/nix-sudo-setuid
Feb 10, 2026
Merged

fix: copy sudo binary before setting setuid bit#9
megheaiulian merged 1 commit intomainfrom
fix/nix-sudo-setuid

Conversation

@megheaiulian
Copy link
Contributor

Summary

  • Fixes /bin/sudo must be owned by uid 0 and have the setuid bit set when using the Nix image in Coder.
  • symlinkJoin creates ./bin/sudo as a symlink into the Nix store. tar (without --dereference) archives symlinks as-is, so the fakeroot-tracked setuid bit on the target is silently lost. This adds cp --remove-destination before chmod 4755 to replace the symlink with a real file copy that tar can faithfully archive with the setuid bit preserved.

Root Cause

buildLayeredImageWithNixDb uses symlinkJoin internally, which creates ./bin/sudo as a symlink pointing to /nix/store/...-sudo-.../bin/sudo. The fakeRootCommands block runs under fakeroot, and the resulting tar is created with --hard-dereference but not --dereference. Since --hard-dereference only affects hard links (not symlinks), the symlink is archived as-is and symlinks in tar don't carry permission bits — the setuid bit set by chmod 4755 is lost.

Fix

Replace the symlink with a real copy of the binary before setting the setuid bit:

cp --remove-destination "$(readlink -f ./bin/sudo)" ./bin/sudo
chmod 4755 ./bin/sudo

symlinkJoin creates ./bin/sudo as a symlink into the Nix store.
tar (without --dereference) archives symlinks as-is, so the
fakeroot-tracked setuid bit on the target is silently lost.

Replace the symlink with a real copy of the binary so that
fakeroot can record mode 4755 on a regular file and tar
faithfully preserves it in the Docker layer.
@megheaiulian megheaiulian merged commit c7bf2bf into main Feb 10, 2026
2 of 3 checks passed
@megheaiulian megheaiulian deleted the fix/nix-sudo-setuid branch February 10, 2026 11:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant